package com.zzyl.nursing.controller;

import com.zzyl.common.utils.PDFUtil;
import com.zzyl.common.utils.file.FileUtils;
import com.zzyl.nursing.dto.HealthAssessmentDto;
import com.zzyl.oss.client.OSSAliyunFileStorageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.nursing.domain.HealthAssessment;
import com.zzyl.nursing.service.IHealthAssessmentService;
import com.zzyl.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 健康评估控制器
 */
@Api(tags = "健康评估相关接口")
@RestController
@RequestMapping("/nursing/healthAssessment")
public class HealthAssessmentController extends BaseController {

    @Autowired
    private IHealthAssessmentService healthAssessmentService;

    @Autowired
    private OSSAliyunFileStorageService fileStorageService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @PreAuthorize("@ss.hasPermi('nursing:healthAssessment:list')")
    @ApiOperation("查询健康评估列表")
    @GetMapping("/list")
    public TableDataInfo list(HealthAssessment healthAssessment) {
        startPage();
        List<HealthAssessment> list = healthAssessmentService.selectHealthAssessmentList(healthAssessment);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('nursing:healthAssessment:query')")
    @ApiOperation("获取健康评估详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@ApiParam(value = "健康评估ID", required = true)
                              @PathVariable("id") Long id) {
        return success(healthAssessmentService.selectHealthAssessmentById(id));
    }

    @PreAuthorize("@ss.hasPermi('nursing:healthAssessment:add')")
    @Log(title = "健康评估", businessType = BusinessType.INSERT)
    @ApiOperation("新增健康评估")
    @PostMapping
    public AjaxResult add(@ApiParam(value = "健康评估实体")
                          @RequestBody HealthAssessmentDto healthAssessmentDto) {
        //新增成功后返回ID，通过ID查询详情，跳转详情页面
        Long id = healthAssessmentService.insertHealthAssessment(healthAssessmentDto);
        return success(id);
    }

    @ApiOperation("上传体检报告")
    @PostMapping("/upload")
    public AjaxResult uploadFile(MultipartFile file, String idCardNo) {
        try {
            //获取上传文件的原始文件名
            String originalFilename = file.getOriginalFilename();
            //使用UUID生成唯一后缀名，以避免文件名重复，并保留原始文件的扩展名
            String fileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
            //将文件上传到阿里云OSS文件存储服务中，并获取存储后的文件URL
            String url = fileStorageService.store(fileName, file.getInputStream());
            //创建一个成功的AjaxResult对象
            AjaxResult ajax = AjaxResult.success();
            //将文件URL和文件名等信息放入AjaxResult对象中
            ajax.put("url", url); //文件URL
            ajax.put("fileName", url); //文件名
            ajax.put("newFileName", FileUtils.getName(fileName)); //新文件名
            ajax.put("originalFilename", file.getOriginalFilename()); //原始文件名
            //PDF文件内容读取为字符串
            String content = PDFUtil.pdfToString(file.getInputStream());
            //将PDF内容临时存储到Redis中，以身份证号为键
            redisTemplate.opsForHash().put("healthReport", idCardNo, content);
            //设置Redis键的过期时间为5分钟
            redisTemplate.expire("healthReport", 5, TimeUnit.MINUTES);
            //返回包含文件上传结果和信息的AjaxResult对象
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
}